home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SDIOLE.PAK / SAMPCONT.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  338 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1994, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/gdiobjec.h>
  8.  
  9. #include <ocf/ocapp.h>
  10. #include <ocf/ocdoc.h>
  11. #include <ocf/ocview.h>
  12. #include <ocf/ocstorag.h>
  13. #include <ocf/ocreg.h>
  14.  
  15. #include <owl/ocfevent.h>
  16. #include <owl/uihelper.h>
  17. #include <owl/printer.h>
  18. #include <owl/preview.h>
  19.  
  20. #include <owl/oleframe.h>
  21. #include <owl/docview.rh>
  22. #include <owl/editfile.rh>
  23.  
  24. #include "sampcont.h"
  25. #define USE_WINDOW_PRINTOUT
  26.  
  27. //----------------------------------------------------------------------------
  28. // Printout class that prints the contents of a window
  29. //
  30. class TWindowPrintout : public TPrintout {
  31.   public:
  32.     TWindowPrintout(const char* title, TWindow* window, bool scale = true)
  33.       : TPrintout(title)
  34.       { Window = window;
  35.         Scale = scale;
  36.         MapMode = MM_ISOTROPIC;    // Respect aspect ratio of window
  37.         //MapMode = MM_ANISOTROPIC;  // Make printout fill the page
  38.       }
  39.  
  40.     void GetDialogInfo(int& minPage, int& maxPage, int& selFromPage, int& selToPage);
  41.     void PrintPage(int page, TRect& rect, unsigned flags);
  42.  
  43.   protected:
  44.     TWindow*  Window;
  45.     bool      Scale;
  46.     int       MapMode;
  47. };
  48.  
  49. //
  50. // Do not enable page range in the print dialog since only one page is
  51. // available to be printed
  52. //
  53. void TWindowPrintout::GetDialogInfo(int& minPage, int& maxPage, int& selFromPage, int& selToPage)
  54. {
  55.   minPage = 0;
  56.   maxPage = 0;
  57.   selFromPage = selToPage = 0;
  58. }
  59.  
  60. //
  61. //
  62. //
  63. void TWindowPrintout::PrintPage(int /*page*/, TRect& bandRect, unsigned /*flags*/)
  64. {
  65.   // Conditionally scale the DC to the window so the printout will
  66.   // resemble the window
  67.   //
  68.   int    oldMode;
  69.   TSize  oldVExt, oldWExt;
  70.   if (Scale) {
  71.     oldMode = DC->SetMapMode(MapMode);
  72.     TRect clientR = Window->GetClientRect();
  73.     DC->SetViewportExt(PageSize, &oldVExt);
  74.     DC->SetWindowExt(clientR.Size(), &oldWExt);
  75.     DC->IntersectClipRect(clientR);
  76.     DC->DPtoLP(bandRect, 2);
  77.   }
  78.  
  79.   // Call the window to paint itself to the printer DC.
  80.   //
  81.   Window->Paint(*DC, FALSE, bandRect);
  82.  
  83.   // Restore changes made to the DC
  84.   //
  85.   if (Scale) {
  86.     DC->SetWindowExt(oldWExt);
  87.     DC->SetViewportExt(oldVExt);
  88.     DC->SetMapMode(oldMode);
  89.   }
  90. }
  91.  
  92. //----------------------------------------------------------------------------
  93.  
  94. //
  95. //
  96. //
  97. class TModalFrame : public TFrameWindow {
  98.   public:
  99.     TModalFrame(TWindow* parent, const char far* title, TWindow* client) :
  100.       TFrameWindow(parent, title, client),
  101.       TWindow(parent, title)  {}
  102.  
  103.     virtual void Destroy(int ret);
  104. };
  105.  
  106. //
  107. //
  108. //
  109. void TModalFrame::Destroy(int ret)
  110. {
  111.   GetApplication()->EndModal(IDCANCEL);
  112.   GetApplication()->MainWindow->EnableWindow(TRUE);
  113.  
  114.   TWindow::Destroy(ret);
  115. }
  116.  
  117. //----------------------------------------------------------------------------
  118.  
  119. DEFINE_RESPONSE_TABLE1(TOleSampContainer, TOleWindow)
  120.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  121.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  122.   EV_COMMAND(CM_FILEPRINT, CmFilePrint),
  123.   EV_COMMAND(CM_FILEPRINTERSETUP, CmFilePrinterSetup),
  124.   EV_COMMAND(CM_FILEPRINTPREVIEW, CmPrintPreview),
  125. END_RESPONSE_TABLE;
  126.  
  127. BEGIN_REGISTRATION(docReg)
  128.   REGDATA(progid,     "SdiOle")
  129.   REGDATA(description,"SdiOle Document")
  130.   REGFORMAT(0, ocrEmbedSource,  ocrContent,  ocrIStorage, ocrGet)
  131.   REGFORMAT(1, ocrMetafilePict, ocrContent,  ocrMfPict|ocrStaticMed, ocrGet)
  132.   REGFORMAT(2, ocrBitmap, ocrContent,  ocrGDI|ocrStaticMed, ocrGet)
  133.   REGFORMAT(3, ocrDib, ocrContent,  ocrHGlobal|ocrStaticMed, ocrGet)
  134.   REGFORMAT(4, ocrLinkSource, ocrContent,  ocrIStream, ocrGet)
  135. END_REGISTRATION
  136.  
  137.  
  138. //
  139. //
  140. //
  141. TOleSampContainer::TOleSampContainer(TWindow*        parent,
  142.                                      const char far* fileName,
  143.                                      TModule*        module)
  144. :
  145.   TOleWindow(parent, module)
  146. {
  147.   // Create a OcDocument object to hold the ole parts that we create
  148.   // and a OcView to provide ole services
  149.   //
  150.   OcDoc = new TOcDocument(*OcApp, fileName);
  151.   OcView = new TOcView(*OcDoc, &docReg);
  152.  
  153.   // Perform actual file loading, and let the OcDoc load its parts
  154.   //
  155.   if (fileName) {
  156.     strcpy(FileData.FileName, fileName);
  157.     OcDoc->LoadParts();
  158.   }
  159.   else {
  160.     strcpy(FileData.FileName, "");
  161.   }
  162.   FileData.Flags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
  163.   FileData.SetFilter(string(*GetModule(), IDS_DOCFILEFILTER).c_str());
  164.   Printer = new TPrinter;
  165. }
  166.  
  167. //
  168. //
  169. //
  170. TOleSampContainer::~TOleSampContainer()
  171. {
  172.   delete Printer;
  173. }
  174.  
  175. //
  176. // Perform normal SetupWindow, plus let the OcView object know our HWND so that
  177. // it can talk to us.
  178. //
  179. void
  180. TOleSampContainer::SetupWindow()
  181. {
  182.   TOleWindow::SetupWindow();
  183.   SetDocTitle(*FileData.FileName ? FileData.FileName : "Untitled", 0);
  184. }
  185.  
  186. //
  187. //
  188. //
  189. void
  190. TOleSampContainer::CleanupWindow()
  191. {
  192.   TOleWindow::CleanupWindow();
  193. }
  194.  
  195. //
  196. //
  197. //
  198. void
  199. TOleSampContainer::CmExit()
  200. {
  201.   OcView->EvClose();
  202.   OcDoc->Close();
  203. //  GetApplication()->CmExit();
  204. }
  205.  
  206. //
  207. // Perform a save operation (or saveAs if doc is untitled) This saves the parts
  208. // and commits the storage
  209. //
  210. void
  211. TOleSampContainer::CmFileSave()
  212. {
  213.   if (!OcDoc->GetName().length()) {
  214.     CmFileSaveAs();
  215.   }
  216.   else {
  217.     OcDoc->SaveParts(0, true);
  218.     OcDoc->GetStorage()->Commit(STGC_DEFAULT);
  219.   }
  220. }
  221.  
  222. //
  223. // Save the document to a new storage file
  224. //
  225. void
  226. TOleSampContainer::CmFileSaveAs()
  227. {
  228.   *FileData.FileName = 0;
  229.   if (TFileSaveDialog(this, FileData).Execute() == IDOK) {
  230.     if (OcDoc->SaveToFile(FileData.FileName)) {
  231.       OcDoc->GetStorage()->Commit(STGC_DEFAULT);
  232.       SetDocTitle(FileData.FileName, 0);
  233.     }
  234.     else
  235.       MessageBox("Cannot save to new file", "File save error", MB_OK);
  236.   }
  237. }
  238.  
  239. //
  240. //
  241. //
  242. void
  243. TOleSampContainer::CmFilePrint()          // Execute File:Print command
  244. {
  245.   if (Printer) {
  246.     TWindowPrintout printout("OLE2 Container", this);
  247.     Printer->Print(this, printout, TRUE);
  248.   }
  249. }
  250.  
  251. //
  252. //
  253. //
  254. void
  255. TOleSampContainer::CmFilePrinterSetup()    // Execute File:Printer-setup command
  256. {
  257.   if (Printer)
  258.     Printer->Setup(this);
  259. }
  260.  
  261. //
  262. //
  263. //
  264. void
  265. TOleSampContainer::CmPrintPreview()
  266. {
  267.   // Create the printer DC. If it fails, let the print dialog report the error
  268.   // for us.
  269.   //
  270.   TPrintDC* prnDC;
  271.   try {
  272.     prnDC = new TPrintDC(Printer->GetSetup().GetDriverName(),
  273.                          Printer->GetSetup().GetDeviceName(),
  274.                          Printer->GetSetup().GetOutputName(),
  275.                          Printer->GetSetup().GetDevMode());
  276.   }
  277.   catch (TXGdi) {
  278.     TPrintDialog(Parent, Printer->GetSetup()).Execute();
  279.     return;
  280.   }
  281.  
  282.   TSize printExtent(prnDC->GetDeviceCaps(HORZRES), prnDC->GetDeviceCaps(VERTRES));
  283.  
  284. #if defined(USE_WINDOW_PRINTOUT)
  285.   TWindowPrintout printout("Print Preview", this);
  286. #else
  287.   TTestPrintout   printout("Print Preview");
  288. #endif
  289.  
  290.   TLayoutWindow* layout = new TLayoutWindow(0);
  291.   layout->SetBkgndColor(GetSysColor(COLOR_APPWORKSPACE));
  292.  
  293.   for (int i = 0; i < 1; i++) {
  294.     TPreviewPage* page = new TPreviewPage(layout, printout, *prnDC, printExtent);
  295.  
  296.     TLayoutMetrics metrics;
  297.     metrics.X.Set(lmLeft, lmRightOf, lmParent, lmLeft, 15);
  298.     metrics.Y.Set(lmTop, lmBelow, lmParent, lmTop, 15);
  299.  
  300.     // Determine major axis of preview page, have that follow parent size.
  301.     // Make minor axis a percentage (aspect ratio) of the page's major axis
  302.     //
  303.     if (printExtent.cx > printExtent.cy) {
  304.       metrics.Width.Set(lmRight, lmLeftOf, lmParent, lmRight, 15);
  305.       metrics.Height.PercentOf(page, int((long(printExtent.cy)*100)/printExtent.cx),
  306.                                lmWidth);
  307.     }
  308.     else {
  309.       metrics.Height.Set(lmBottom, lmAbove, lmParent, lmBottom, 15);
  310.       metrics.Width.PercentOf(page, int((long(printExtent.cx)*100)/printExtent.cy),
  311.                               lmHeight);
  312.     }
  313.  
  314.     layout->SetChildLayoutMetrics(*page, metrics);
  315.   }
  316.  
  317.   TFrameWindow* frame = new TModalFrame(this, "Preview", layout);
  318.   frame->Create();
  319.   frame->ShowWindow(SW_SHOWNORMAL);
  320.  
  321.   GetApplication()->BeginModal(Parent);
  322.   delete prnDC;
  323. }
  324.  
  325. //
  326. // See if we can close. Also let OcView know we are going away
  327. //
  328. bool
  329. TOleSampContainer::CanClose()
  330. {
  331.   if (!TOleWindow::CanClose())
  332.     return false;
  333.  
  334.   OcView->EvClose();
  335.   OcDoc->Close();
  336.   return true;
  337. }
  338.